package com.tjp.eight.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.view.WindowManager;

import com.tjp.eight.WApplication;

/**
 * Created by Administrator on 2017/3/27.
 */
public class ApplicationUtil {

    public static int getScreenWidth(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return windowManager.getDefaultDisplay().getWidth();

    }

    public static int getScreenH(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return windowManager.getDefaultDisplay().getHeight();

    }

    private static Dialog mDialog;

    public static Context getApplicationContext() {
        return WApplication.getInstance().getApplicationContext();
    }

    /**
     * 检查网络状态
     *
     * @param ctx
     * @return
     */
    public final static boolean isNetWorkConnected(Context ctx) {
        ConnectivityManager manager = (ConnectivityManager) ctx
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo network = manager.getActiveNetworkInfo();
        if (network != null && network.isConnected()) {
            if (network.getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }
        }
        return false;
    }

    /**
     * 得到app版本名称
     *
     * @param context
     * @return
     */
    public static String getVerName(Context context) {
        String verName = "";
        try {
            verName = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return verName;
    }

    public static String getV() {
        return getVerName(WApplication.getInstance()) + "." + getVerCode(WApplication.getInstance());
    }


    private static void AppExit(Context context) {
        try {
            Intent intent = new Intent();
            intent.setAction(context.getApplicationContext().getPackageName()
                    + ".ExitApplication");
            context.sendBroadcast(intent);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    System.exit(0);
                }
            }, 200);
        } catch (Exception e) {
        }
    }


    public static int stringToGeopoint(double v) {
        // int) (39.918* 1E6)
        int i = 0;
        try {
            double temp = v;
            i = (int) (temp * 1E6);
        } catch (Exception ex) {
            i = 0;
        }
        return i;
    }

    /**
     * 获取版本号
     *
     * @param context 上下文
     * @return 当前版本号
     */
    public static int getVerCode(Context context) {
        int verCode = -1;
        try {
            verCode = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), 0).versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return verCode;
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
